﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mailer.Model.Repositories;
using System.Text.RegularExpressions;
using NHibernate;
using Mailer.Model;
using Mailer.Model.Types;
using System.Collections;
using NHibernate.Criterion;

namespace Mailer.Controllers
{
	[HandleError]
	public class PublicListController : Controller
	{

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Subscribe(Guid id)
		{
			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				List list = sess.Get<List>(id);
				if (list != null)
				{
					Application app = sess.Get<Application>(list.ApplicationID);

					ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
					ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
					ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
					ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");

					ViewData["listname"] = list.Name;
				}

				SessionUtilities.EndSession(sess);
			}

			return base.View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Subscribe(Guid id, string name, string email)
		{
			ActionResult res = null;

			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{

				List list = sess.Get<List>(id);
				Application app = sess.Get<Application>(list.ApplicationID);

				ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
				ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
				ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
				ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");

				ViewData["listname"] = list.Name;

				using (ITransaction transaction = sess.BeginTransaction())
				{
					Recipient recip = sess.CreateCriteria<Recipient>().Add(Expression.Eq("Email", email)).Add(Expression.Eq("ApplicationID", app.ID)).UniqueResult<Recipient>();
					if (recip != null)
					{
						recip.UpdatedBy = Guid.Empty;
						recip.UpdatedDate = DateTime.Now;
						recip.State = (int)RecipientState.Verified;
						if (!recip.Lists.Contains(list))
							recip.Lists.Add(list);
						sess.Save(recip);

						if (!string.IsNullOrEmpty(name))
							recip.SetPropertyValue(sess, "displayname", name);
					}
					else
					{
						Recipient newrecip = new Recipient();
						newrecip.ApplicationID = app.ID;
						newrecip.Lists.Add(list);
						newrecip.Email = email;
						newrecip.CreatedBy = Guid.Empty;
						newrecip.CreatedDate = DateTime.Now;
						newrecip.UpdatedBy = Guid.Empty;
						newrecip.UpdatedDate = DateTime.Now;
						newrecip.State = (int)RecipientState.Verified;
						sess.Save(newrecip);

						if (!string.IsNullOrEmpty(name))
							newrecip.SetPropertyValue(sess, "displayname", name);
					}

					ViewData["email"] = "";
					ViewData["firstname"] = "";
					ViewData["lastname"] = "";

					// TODO: skicka infomail

					// ModelState.Add(  .AddModelError("_FORM", ErrorCodeToString(createStatus));

					res = RedirectToAction("SubscribeThanks", new { id = id });

					transaction.Commit();
				}

				SessionUtilities.EndSession(sess);
			}

			// If we got this far, something failed, redisplay form
			if (res == null)
				res = View();
			return res;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult SubscribeThanks(Guid id)
		{
			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				List list = sess.Get<List>(id);
				if (list != null)
				{
					Application app = sess.Get<Application>(list.ApplicationID);

					ViewData["listname"] = list.Name;

					ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
					ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
					ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
					ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");
				}

				SessionUtilities.EndSession(sess);
			}

			return base.View();
		}









		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Unsubscribe(Guid id)
		{
			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				List list = sess.Get<List>(id);
				if (list != null)
				{
					Application app = sess.Get<Application>(list.ApplicationID);

					ViewData["listname"] = list.Name;

					ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
					ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
					ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
					ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");
				}

				SessionUtilities.EndSession(sess);
			}
			return base.View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Unsubscribe(Guid id, string email)
		{
			ActionResult res = null;

			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				List list = sess.Get<List>(id);
				if (list != null)
				{
					Application app = sess.Get<Application>(list.ApplicationID);

					ViewData["listname"] = list.Name;

					ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
					ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
					ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
					ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");

					using (ITransaction transaction = sess.BeginTransaction())
					{

						Recipient recip = sess.CreateCriteria<Recipient>().Add(Expression.Eq("Email", email)).Add(Expression.Eq("ApplicationID", app.ID)).UniqueResult<Recipient>();
						if (recip != null)
						{
							recip.UpdatedBy = Guid.Empty;
							recip.UpdatedDate = DateTime.Now;
							
							if (recip.Lists.Contains(list))
								recip.Lists.Remove(list);
							
							sess.Save(recip);
						}

						transaction.Commit();
					}

					// TODO: skicka infomail

					res = RedirectToAction("UnsubscribeThanks", new { id = id });
				}

				SessionUtilities.EndSession(sess);
			}
			if (res == null)
				res = View();
			return res;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult UnsubscribeThanks(Guid id)
		{
			ISession sess = SessionUtilities.BeginSession();
			if (sess != null)
			{
				List list = sess.Get<List>(id);
				if (list != null)
				{
					Application app = sess.Get<Application>(list.ApplicationID);

					ViewData["listname"] = list.Name;

					ViewData["userstylesheet"] = app.GetPropertyValue("userstylesheet");
					ViewData["userheadhtml"] = app.GetPropertyValue("userheadhtml");
					ViewData["usertophtml"] = app.GetPropertyValue("usertophtml");
					ViewData["userbottomhtml"] = app.GetPropertyValue("userbottomhtml");
				}

				SessionUtilities.EndSession(sess);
			}

			return base.View();
		}






	}
}
